from django.shortcuts import render
from rest_framework.viewsets import GenericViewSet
from utils.common_view import CacheListModelMixin, CommonListModelMixin, CommonRetrieveModelMixin
from .models import CourseCategory, Course, CourseChapter
from .serializer import CourseCategorySerializer, CourseSerializer, CourseChapterSerializer, CourseDetailSerializer
from rest_framework.filters import OrderingFilter
from django_filters.rest_framework import DjangoFilterBackend
from utils.common_pagination import CommonPagination as PageNumberPagination


class CourseCategoryView(GenericViewSet, CacheListModelMixin):
    queryset = CourseCategory.objects.all().filter(is_show=True, is_delete=False).order_by('orders')
    serializer_class = CourseCategorySerializer
    cache_key = 'cache_course_category'


# class CourseView(GenericViewSet, CommonListModelMixin, CommonRetrieveModelMixin):
#     queryset = Course.objects.all().filter(is_show=True, is_delete=False).order_by('orders')
#     serializer_class = CourseSerializer
#
#     # 排序 ：price,students
#     filter_backends = [OrderingFilter, DjangoFilterBackend]
#     ordering_fields = ['price', 'students']
#     filterset_fields = ['course_category']
#     pagination_class = PageNumberPagination

# 查看课程详情：方式一
class CourseChapterView(GenericViewSet, CommonListModelMixin):
    queryset = CourseChapter.objects.all().filter(is_show=True, is_delete=False).order_by('orders')
    serializer_class = CourseChapterSerializer
    filter_backends = [DjangoFilterBackend]
    filterset_fields = ['course']


# 查看课程详情：方式二
class CourseView(GenericViewSet, CommonListModelMixin, CommonRetrieveModelMixin):
    queryset = Course.objects.all().filter(is_show=True, is_delete=False).order_by('orders')
    serializer_class = CourseSerializer

    # 排序 ：price,students
    filter_backends = [OrderingFilter, DjangoFilterBackend]
    ordering_fields = ['price', 'students']
    filterset_fields = ['course_category']
    pagination_class = PageNumberPagination

    # 通过重写 get_serializer_class  改变序列化类
    def get_serializer_class(self):
        if self.action == 'retrieve':
            return CourseDetailSerializer
        else:
            return CourseSerializer


############ 搜索接口 ############
from rest_framework.filters import SearchFilter


class SearchCourseView(GenericViewSet, CommonListModelMixin, CommonRetrieveModelMixin):
    queryset = Course.objects.all().filter(is_show=True, is_delete=False).order_by('orders')
    serializer_class = CourseSerializer
    filter_backends = [SearchFilter]
    search_fields = ['name']
    pagination_class = PageNumberPagination

    '''
       这个搜索接口非常简单--》只搜索了实战课
       实际上：我们要搜课程--》输入关键字
           -实战课
           -免费课
           -轻课
           
    def list(self, request, *args, **kwargs):
        # 1 实战课数据
        res=super().list(request, *args, **kwargs)
        # 2 免费课数据  request 中取出查询条件--》免费课表中查出来，序列化
        free_list=[{},{}]
        # 3 轻课数据  request 中取出查询条件--》轻课表中查出来，序列化
        light_list = [{}, {}]

        # 4 假设带了老师数据

        # 5 我们搜索，都是直接搜的mysql，数据量越大，效率越低
            # 大数据量搜索：elasticsearch 分布式全文检索引擎--》搜索建议，个性化推荐
            # 我们只要mysql 中课程数据变量---》同步到es中
            # 以后搜:直接从es中搜
        return APIResponse(actual_list=res.results,free_list=free_list,light_list=light_list)
    
    '''
